﻿package  
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author chancidal
	 */
	public class Main extends Sprite
	{
		//
		private var m_startBall:Ball;
		private var m_centerBall:Ball;
		private var m_endBall:Ball;
		private var m_line:Shape;
		//
		public function Main() 
		{
			init();
		}
		private function init():void {
			m_line = new Shape();
			addChild(m_line);
			m_startBall = new Ball();
			m_centerBall = new Ball();
			m_endBall = new Ball();
			addChild(m_startBall);
			m_startBall.x = m_startBall.y = 50;
			addChild(m_centerBall);
			m_centerBall.x = 80;
			m_centerBall.y = 50;
			addChild(m_endBall);
			m_endBall.x = 200;
			m_endBall.y = 150;
			drawLine();
			m_startBall.addEventListener(MouseEvent.MOUSE_DOWN, dragBall);
			m_centerBall.addEventListener(MouseEvent.MOUSE_DOWN, dragBall);
			m_endBall.addEventListener(MouseEvent.MOUSE_DOWN, dragBall);
		}
		//////////////////////////////////////////////////拖动小球
		private function dragBall(evt:MouseEvent):void {
			evt.target.startDrag();
			evt.target.addEventListener(Event.ENTER_FRAME, drawLine);
			evt.target.addEventListener(MouseEvent.MOUSE_UP, stopDragBall);
		}
		//////////////////////////////////////////////////停止拖动小球
		private function stopDragBall(evt:Event):void {
			evt.target.removeEventListener(Event.ENTER_FRAME, drawLine);
			evt.target.removeEventListener(MouseEvent.MOUSE_UP, stopDragBall);
			evt.target.stopDrag();
		}
		//////////////////////////////////////////////////划线函数
		private function drawLine(evt:Event = null):void {
			m_line.graphics.clear();
			m_line.graphics.lineStyle(1, 0x0000ff);
			m_line.graphics.moveTo(m_startBall.x, m_startBall.y);
			var controlX:Number = 2 * m_centerBall.x - (m_endBall.x + m_startBall.x) * .5;
			var controlY:Number = 2 * m_centerBall.y - (m_endBall.y + m_startBall.y) * .5;
			m_line.graphics.curveTo(controlX, controlY, m_endBall.x, m_endBall.y);
		}
	}

}
class Ball extends flash.display.Sprite {
	public function Ball() {
		graphics.beginFill(0);
		graphics.drawCircle(0, 0, 3);
	}
}